package es.udc.fbellas.j2ee.tiendaweb.http.controller.actions;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import es.udc.fbellas.j2ee.tiendaweb.http.view.actionforms.FindProductForm;
import es.udc.fbellas.j2ee.tiendaweb.model.productofacade.TO.ProductoChunkTO;
import es.udc.fbellas.j2ee.tiendaweb.model.productofacade.delegate.ProductoFacadeDelegateFactory;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;

public class FindProductAction extends DefaultAction {
	
    public ActionForward doExecute(ActionMapping mapping,
        
    	
    	ActionForm form, HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException, InternalErrorException {

        /* Get data. */
    	FindProductForm findProductForm = (FindProductForm) form;
        int identifier = findProductForm.getIdentifier();
        String name = findProductForm.getNameProduct();
        int inicio = findProductForm.getStartIndex();
        int numero = findProductForm.getCount();
        int idCategory = findProductForm.getIdCategory(); 
        
        
        /* Do action. */
//        if (FindProductForm.PRODUCT_IDENTIFIER.equals(identifier)) {
            return doFindProductByName(mapping, name, inicio, numero, request);
//        }
//       else {
//            return doFindProductByNameCategory(mapping, name, idCategory, inicio, numero, request);
//        }
        
    }
    
    
    private ActionForward doFindProductByName(
        ActionMapping mapping, String name, int start, int count,
        HttpServletRequest request) throws InternalErrorException {
    	
//        try {        
        
            ProductoChunkTO productoChunkTO =
                ProductoFacadeDelegateFactory.getDelegate().buscarPorPalabras(name, start, count);

            
            if (productoChunkTO.getProductoTOs().size() > 0) {
                request.setAttribute("products", productoChunkTO.getProductoTOs());
            }
            
            /* Generate parameters for previous and next links. */
            Map previousLinkParameters = getPreviousLinkParameters(name, start, count);
            if (previousLinkParameters != null) {

                request.setAttribute("previous", previousLinkParameters);
            }
                    
            if (productoChunkTO.getExistMasProductos()) {
                Map nextLinkParameters = getNextLinkParameters(name, start, count);        
                
                request.setAttribute("next", nextLinkParameters);
                
            }
        
            /* Return ActionForward. */    
            return mapping.findForward("FindProductByName");            
            
            
//                    
//        } catch (InstanceNotFoundException e) {
//            
//            ActionMessages errors = new ActionMessages();
//            
//            errors.add("identifier",
//                new ActionMessage("ErrorMessages.product.notFound"));
//            saveErrors(request, errors);
//            
//            return new ActionForward(mapping.getInput());
//            
//        }    
        
    }
    
    private ActionForward doFindProductByNameCategory(
        ActionMapping mapping, String name, int idCategory, int startIndex,
        int count, HttpServletRequest request) throws InternalErrorException {
                
        /* Find accounts by user identifier. */                    
        ProductoChunkTO productoChunkTO =
            ProductoFacadeDelegateFactory.getDelegate().buscarPorPalabrasYCategoria(name, idCategory, startIndex, count);
                
        if (productoChunkTO.getProductoTOs().size() > 0) {
            request.setAttribute("products", productoChunkTO.getProductoTOs());
        }
        
        /* Generate parameters for previous and next links. */
        Map previousLinkParameters = getPreviousLinkParameters(name,startIndex, count);
        if (previousLinkParameters != null) {
            request.setAttribute("previous", previousLinkParameters);
        }
                
        if (productoChunkTO.getExistMasProductos()) {
            Map nextLinkParameters = getNextLinkParameters(name, 
                startIndex, count);        
            request.setAttribute("next", nextLinkParameters);
        }
    
        /* Return ActionForward. */    
        return mapping.findForward("FindProductByNameCategory");
        
    }
    
    private Map getPreviousLinkParameters(String nameProduct, int startIndex, 
        int count) {        
        
        Map linkAttributes = null;
                
        if ( (startIndex-count) > 0 ) {
        
            linkAttributes = getCommonPreviousNextLinkParameters(nameProduct,
                startIndex, count);
            linkAttributes.put("startIndex", new Integer(startIndex-count));
            
        }
        
        return linkAttributes;
        
    }
    
    private Map getNextLinkParameters(String nameProduct, int startIndex, 
        int count) {        
        
        Map linkAttributes = getCommonPreviousNextLinkParameters(nameProduct,
            startIndex, count);

        linkAttributes.put("startIndex", new Integer(startIndex+count));            

        return linkAttributes;
        
    }
    
    private Map getCommonPreviousNextLinkParameters(String nameProduct, 
        int startIndex, int count) {
        
        Map linkAttributes = new HashMap();
        

        linkAttributes.put("nameProduct", nameProduct);
        linkAttributes.put("count", new Integer(count));
        
        return linkAttributes;
        
    }

}

